set scheme s1mono
set matsize 10000
capture cd "C:\Users\bhavnani\Desktop\Dropbox\Research\India malapportionment and development\Analysis"
capture cd "C:\Users\Rikhil Bhavnani\Desktop\Dropbox\Research\India malapportionment and development\Analysis"

use "Bhavnani 2021 PLOS replication data", clear

*********
*Figure 1
*********
preserve
	collapse2 (mean) lndistrriimp, by(st_name dist_name afterdelim)
	reshape wide lndistrriimp, i(st_name dist_name) j(afterdelim)
	twoway (scatter lndistrriimp1 lndistrriimp0, msize(tiny)) || (lfit lndistrriimp1 lndistrriimp0), legend(off) xtitle("Pre-redistricting ln RRI", size(large)) ytitle("Post-redistricting ln RRI", size(large)) ylabel(-1 -0.5 0 0.5, labsize(large)) xlabel(,labsize(large)) 
restore

*********
*Figure 2
*********
preserve
	label var lndistrriimp "Ln RRI, 2004"
	foreach var in purban2001 secc_pov_rate {
		scatter `var' lndistrriimp if year==2004 & abs(lndistrriimp)<=.5, msize(tiny) xtitle(, size(vlarge)) ytitle(, size(vlarge)) name(`var', replace) 
	}
	graph combine purban2001 secc_pov_rate
restore

*********
*Figure 3
*********
xi: areg lnnightlights lndistrriimp2004 lndistrriimp2005 lndistrriimp2006 lndistrriimp2007 lndistrriimp2008 lndistrriimp2009 lndistrriimp2010 lndistrriimp2011 lndistrriimp2012 lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
coefplot, keep(lndistrriimp2004 lndistrriimp2005 lndistrriimp2006 lndistrriimp2007 lndistrriimp2008 lndistrriimp2009 lndistrriimp2010 lndistrriimp2011 lndistrriimp2012) vertical yline(0) ytitle("Coefficient on Ln RRI X year dummies") 

*********
*Figure 4 
*********
*propcoalmem 
eststo: xi: areg lnnightlights lndistrriimp lndistrriimppropcoalmem propcoalmem lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
capture drop where
capture drop pipe
gen where=0
gen pipe="|"
summ propcoalmem if e(sample)
global max = r(max)
global min = r(min)
mat V=e(V)
local var1=V[1,1]
local var2=V[2,2]
local covar=V[2,1]
twoway (function y = _b[lndistrriimp] + _b[lndistrriimppropcoalmem]*x, range($min $max)) ///
(function y = _b[lndistrriimp] + _b[lndistrriimppropcoalmem]*x + 1.96*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(function y = _b[lndistrriimp] + _b[lndistrriimppropcoalmem]*x - 1.96*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(scatter where propcoalmem if e(sample), ms(none) mlabel(pipe) mlabpos(0) legend(off)), ///
ytitle("% change in light output" "per % change in RRI", size(large)) xtitle("Pr. rep. in gov. coal.", size(large)) name(z1, replace) ///
legend(off) ylabel(,valuelabel labs(large) nogrid) xlabel(,labsize(large)) 

*tv_rural
eststo: xi: areg lnnightlights lndistrriimp lndistrriimptv_rural lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
capture drop where
capture drop pipe
gen where=0
gen pipe="|"
summ tv_rural if e(sample)
global max = r(max)
global min = r(min)
mat V=e(V)
local var1=V[1,1]
local var2=V[2,2]
local covar=V[2,1]
twoway (function y = _b[lndistrriimp] + _b[lndistrriimptv_rural]*x, range($min $max)) ///
(function y = _b[lndistrriimp] + _b[lndistrriimptv_rural]*x + 1.96*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(function y = _b[lndistrriimp] + _b[lndistrriimptv_rural]*x - 1.96*(`var1'+x^2*`var2'+2*x*`covar')^0.5, range($min $max) lpattern("-")) ///
(scatter where tv_rural  if e(sample), ms(none) mlabel(pipe) mlabpos(0) legend(off)), ///
ytitle("% change in light output" "per % change in RRI", size(large)) xtitle("Pr. that own TVs", size(large)) name(z4, replace) ///
legend(off) ylabel(,valuelabel labs(large) nogrid) xlabel(,labsize(large)) 
graph combine z1 z4

********
*Table 1
********
reg lnnightlights lndistrriimp lnelectorsimp Llnnightlights
xi: areg lnnightlights lndistrriimp lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp lndistrriimppost2008 lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)

*****************
*Appendix Table 1
*****************
tab st_name if year==2004

*****************
*Appendix Table 2
***************** 
summ lnnightlights Gnightlights lndistrriimp lnelectorsimp propcoalmem tv_rural, sep(0)

*****************
*Appendix Table 3
*****************
xi: areg lnnightlights lndistrriimp lndistrriimppost2008 lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp lndistrriimppost lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)

*****************
*Appendix Table 4
*****************
reg lnunderimplementation lndistrriimp lnelectorsimp Llnunderimplementation
xi: areg lnunderimplementation lndistrriimp lnelectorsimp Llnunderimplementation i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnunderimplementation lndistrriimp lndistrriimppost2008 lnelectorsimp Llnunderimplementation i.stateyear, a(statedistcoded) cl(stateyear)

*****************
*Appendix Table 5
*****************
xi: areg Gnightlights lndistrriimp lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lnseats lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp lnelectorsimp i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp propcoalmem lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)

*****************
*Appendix Table 6
*****************
xi: areg lnnightlights lndistrriimp lndistrriimppropcoalmem propcoalmem lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp lndistrriimptv_rural lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)
xi: areg lnnightlights lndistrriimp lndistrriimppropcoalmem lndistrriimptv_rural propcoalmem lnelectorsimp Llnnightlights i.stateyear, a(statedistcoded) cl(stateyear)

